package com.flexdms.i18n;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.config.ConfigProperty;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
@ApplicationScoped
@Alternative
public class BingTranslator extends BaseTranslator
{
	@Inject
	@ConfigProperty(name="com.flexdms.i18n.bingtranslator.clientid")
	String clientID;
	
	@Inject
	@ConfigProperty(name="com.flexdms.i18n.bingtranslator.secretkey")
	String secretkey;
	

	public void setClientID(String clientID)
	{
		this.clientID = clientID;
	}

	public void setSecretkey(String secretkey)
	{
		this.secretkey = secretkey;
	}

	@PostConstruct
	public void init()
	{
		 Translate.setClientId(clientID);
		  Translate.setClientSecret(secretkey);
	}
	
	@Override
	protected List<String> _translate(List<String> qStrings, Locale locale)
			throws Exception
	{
		String[] resultsStrings=Translate.execute(qStrings.toArray(new String[0]), localeToLanguage(getSourceLocale()), localeToLanguage(locale));
		return Arrays.asList(resultsStrings);

	}
	
	protected Language localeToLanguage(Locale locale)
	{
		String lang=locale.getLanguage().toLowerCase();
		if (lang.equalsIgnoreCase("zh"))
		{
			if (locale.getCountry().equalsIgnoreCase("TW"))
			{
				lang="zh-CHT";
			} else 
			{
				lang="zh-CHS";
			}
			
		}
		return Language.fromString(lang);
	}

}
